<?php
/**
 * Created by PhpStorm.
 * User: boye009
 * Date: 2016/3/22
 * Time: 15:49
 */

namespace app\bbs\api;

use app\bbs\model\ForumPostLike;
use app\common\api\BaseApi;

class ForumPostLikeApi extends BaseApi{

    /**
     * 添加
     */
    const ADD = "bbs/ForumPostLikeApi/add";
    /**
     * 保存
     */
    const SAVE = "bbs/ForumPostLikeApi/save";
    /**
     * 保存根据ID主键
     */
    const SAVE_BY_ID = "bbs/ForumPostLikeApi/saveByID";

    /**
     * 删除
     */
    const DELETE = "bbs/ForumPostLikeApi/delete";

    /**
     * 查询
     */
    const QUERY = "bbs/ForumPostLikeApi/query";
    const QUERY_WITH_USER = "bbs/ForumPostLikeApi/queryWithUser";
    /**
     * 查询一条数据
     */
    const GET_INFO = "bbs/ForumPostLikeApi/getInfo";

    /**
     * 插入多条数据
     */
    const ADD_ALL = "bbs/ForumPostLikeApi/addAll";

    /**
     * 无分页查询
     */
    const QUERY_NO_PAGING = "bbs/ForumPostLikeApi/queryNoPaging";

    /**
     * 统计
     */
    const COUNT = "bbs/ForumPostLikeApi/count";

    protected function _init(){
        $this -> model = new ForumPostLike();
    }


    /**
     * query
     * @param 查询条件|null $map
     * @param array|分页参数 $page
     * @param bool|排序参数 $order
     * @param bool|点击分页时带参数 $params
     * @param bool $fields
     * @return array
     * @internal param 查询条件 $map
     * @internal param 分页参数 $page
     * @internal param 排序参数 $order
     * @internal param 点击分页时带参数 $params
     */
    public function queryWithUser($map = null, $page = ['curpage'=>1,'size'=>10], $order = 'like_time desc', $params = false, $fields = false) {
        $query = $this->model->alias('p');
        if(!empty($map)) $query = $query->where($map);
        if(false !== $order) $query = $query->order($order);
        if(false !== $fields) $query = $query->field($fields);
        $list = $query ->join('left join common_member as m on m.uid=p.uid')
        ->join('left join itboye_forum_thread as t on t.tid=p.tid')
        // -> page($page['curpage'] . ',' . $page['size'])
        ->limit(max(intval($page['curpage'])-1,0)*$page['size'],$page['size'])
        ->select();
        if (false === $list) $this -> apiReturnErr($this -> model -> getDbError());

        $count = $this -> model ->alias('p')->join('left join itboye_forum_thread as t on t.tid=p.tid') -> where($map) -> count();
        return $this -> apiReturnSuc(["count" => $count, "list" => $list]);
    }

}